package flc.ast.utils;

import android.content.Context;
import android.graphics.*;
import android.graphics.pdf.PdfDocument;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageToPdfUtils {
    private static final int PDF_PAGE_WIDTH = 595; // A4宽度(单位:pt)
    private static final int MAX_PAGE_HEIGHT = 842; // 单页最大高度

    public static void convertImageToPdf(Context context, String imagePath, File outputFile) throws IOException {
        // 1. 获取图片原始尺寸
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);
        int imgWidth = options.outWidth;
        int imgHeight = options.outHeight;

        // 2. 计算缩放比例保持宽度不变
        float scaleFactor = (float)PDF_PAGE_WIDTH / imgWidth;
        int scaledHeight = (int)(imgHeight * scaleFactor);

        // 3. 初始化PDF文档
        PdfDocument document = new PdfDocument();
        int currentY = 0;

        // 4. 分页处理
        while (currentY < scaledHeight) {
            int pageHeight = Math.min(MAX_PAGE_HEIGHT, scaledHeight - currentY);

            // 创建PDF页面
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(
                    PDF_PAGE_WIDTH, pageHeight, document.getPages().size() + 1).create();
            PdfDocument.Page page = document.startPage(pageInfo);

            // 绘制当前页内容
            drawImageSegment(page.getCanvas(), imagePath,
                    imgWidth, imgHeight, currentY, scaleFactor);
            document.finishPage(page);

            currentY += pageHeight;
        }

        // 5. 输出PDF
        try (FileOutputStream fos = new FileOutputStream(outputFile)) {
            document.writeTo(fos);
        }
        document.close();
    }

    private static void drawImageSegment(Canvas canvas, String imagePath,
                                         int imgWidth, int imgHeight, int currentY, float scaleFactor) {
        // 计算源图像裁剪区域
        int srcTop = (int)(currentY / scaleFactor);
        int srcBottom = (int)((currentY + MAX_PAGE_HEIGHT) / scaleFactor);
        srcBottom = Math.min(srcBottom, imgHeight);

        // 加载并绘制图片片段
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = calculateInSampleSize(imgWidth, srcBottom - srcTop);
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

        if (bitmap != null) {
            Rect srcRect = new Rect(0, srcTop, imgWidth, srcBottom);
            Rect destRect = new Rect(0, 0, PDF_PAGE_WIDTH,
                    (int)((srcBottom - srcTop) * scaleFactor));
            canvas.drawBitmap(bitmap, srcRect, destRect, null);
            bitmap.recycle();
        }
    }

    private static int calculateInSampleSize(int width, int height) {
        int inSampleSize = 1;
        while (width / inSampleSize > 2048 || height / inSampleSize > 2048) {
            inSampleSize *= 2;
        }
        return inSampleSize;
    }
}
